'use strict';
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const glob = require("glob")
const debug = process.argv.indexOf('-d') !== -1

var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

// let plugins = [
//     new webpack.optimize.UglifyJsPlugin({
//         compress: {
//             warnings: debug,
//         },
//         output: {
//             comments: false,
//         },
//     }),
//     new ExtractTextPlugin("[name].css")
// ]

let plugins = [
    new webpack.LoaderOptionsPlugin({
        options: {
            babel: {
                presets: ['es2015'],  // ���ý�es6�﷨ת����es5�﷨
                plugins: ['transform-runtime']
            }
        }
    }),
    new webpack.optimize.UglifyJsPlugin({
        compress: {
            warnings: debug,
        },
        output: {
            comments: false,
        },
    }),
    new ExtractTextPlugin("[name].css")
]

let globmaths = glob.sync("./src/pages/**/*.js", {
    nodir: true,
})

let entry = globmaths.reduce((obj, file) => {

    let buffpath = path.dirname(file);

    var pageIndex = buffpath.indexOf('pages') + 5;

    let endPath = buffpath.substring(pageIndex);

    let filename = path.basename(file).split(".")[0];

    let outFileName = endPath + '/' + filename;

    obj[outFileName] = ["babel-polyfill", file];

    return obj;

}, {})

module.exports = {
    entry: entry,
    // entry:getEntry(),
    output: {
        // path: './lib',
        path: path.resolve(__dirname, './lib'),
        filename: "[name].js",
        publicPath: '../../../lib/'
    },
    module: {
        loaders: [
            { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
            { test: /\.vue$/, exclude: /node_modules/, loader: "vue-loader" },
            {
                test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192&name=images/[hash:8].[name].[ext]'
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
                // loader:'style-loader!css-loader'
            }
        ]
    },
    resolve: {
        alias: {
            'vue': 'vue/dist/vue.js',
            image: process.cwd() + '/src/images'
        }
    },
    plugins: plugins
};
